package com.sangmo.fts.process.inventory.calculator;

import com.sangmo.boot.framework.common.utils.ApplicationUtils;
import com.sangmo.boot.framework.data.json.context.KeyValue;
import com.sangmo.boot.framework.data.json.load.Calculator;
import com.sangmo.fts.process.inventory.model.IfLoan;
import com.sangmo.fts.process.inventory.model.IfRepayment;
import com.sangmo.fts.process.inventory.repo.IfRepaymentRepo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@Component
public class IfLoanRepaymentAmountCalculator implements Calculator<IfLoan>, IfLoan.IIfLoanRepaymentAmountCalculator {

    @Override
    public Object calc(IfLoan bean, Collection<KeyValue> exts) {
        IfRepaymentRepo ifRepaymentRepo = ApplicationUtils.getBean(IfRepaymentRepo.class);
        List<IfRepayment> repayments = ifRepaymentRepo.findByLoanId(bean.getId());
        BigDecimal repaymentAmount = BigDecimal.ZERO;
        for (IfRepayment repayment : repayments) {
            repaymentAmount = repaymentAmount.add(repayment.getAmount());
        }
        return repaymentAmount;
    }
}
